/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.vfi.bc;

import edu.upn.sigemfi.seg.bc.PersonaFacadeLocal;
import edu.upn.sigemfi.seg.bc.PersonaJuridicaFacadeLocal;
import edu.upn.sigemfi.seg.bc.PersonaNaturalFacadeLocal;
import edu.upn.sigemfi.seg.bc.EmpleadoFacadeLocal;
import edu.upn.sigemfi.seg.be.PersonaNatural;
import edu.upn.sigemfi.seg.be.PersonaJuridica;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Alex
 */
public class PersonaDelegate {

    private PersonaFacadeLocal personaLocal;
    private PersonaNaturalFacadeLocal personaNaturalLocal;
    private PersonaJuridicaFacadeLocal personaJuridicaLocal;
    private EmpleadoFacadeLocal empleadoLocal;

    private PersonaNatural A;
 

    public PersonaDelegate() {
        personaLocal = lookupPersonaFacade();
        personaNaturalLocal = lookupPersonaNaturalFacade();
        personaJuridicaLocal = lookupPersonaJuridicaFacade();
    }

    public void crearPersonaNatural(PersonaNatural personaNatural) throws Exception {
        try {
            personaNaturalLocal.create(personaNatural);
        } catch (Exception e) {
            personaNaturalLocal = lookupPersonaNaturalFacade();
            throw new Exception("Crear PersonaNatural. " + e.getMessage(), e);
        }
    }

    public void eliminarPersonaNatural(PersonaNatural personaNatural) throws Exception {
        try {
            personaNaturalLocal.remove(personaNatural);
        } catch (Exception e) {
            personaNaturalLocal = lookupPersonaNaturalFacade();
            throw new Exception("Eliminar PersonaNatural. " + e.getMessage(), e);
        }
    }

    public void editarPersonaNatural(PersonaNatural personaNatural) throws Exception {
        try {
            personaNaturalLocal.edit(personaNatural);
        } catch (Exception e) {
            personaNaturalLocal = lookupPersonaNaturalFacade();
            throw new Exception("Editar PersonaNatural. " + e.getMessage(), e);
        }
    }

    public PersonaNatural buscarPersonaNatural(long codigo) throws Exception {
        try {
            return personaNaturalLocal.find(codigo);
        } catch (Exception e) {
            personaNaturalLocal = lookupPersonaNaturalFacade();
            throw new Exception("Buscar PersonaNatural. " + e.getMessage(), e);
        }
    }

    public List<PersonaNatural> ListarPersonaNaturales() throws Exception {
        try {
            return personaNaturalLocal.findAll();
        } catch (Exception e) {
            personaNaturalLocal = lookupPersonaNaturalFacade();
            throw new Exception("Listar PersonaNatural. " + e.getMessage(), e);
        }
    }


//--------------------------------------PERSONAS JURIDICAS
    public void crearPersonaJuridica(PersonaJuridica personaJuridica) throws Exception {
        try {
            personaJuridicaLocal.create(personaJuridica);
        } catch (Exception e) {
            personaJuridicaLocal = lookupPersonaJuridicaFacade();
            throw new Exception("Crear Persona Juridica. " + e.getMessage(), e);
        }
    }

    public void eliminarPersonaJuridica(PersonaJuridica personaJuridica) throws Exception {
        try {
            personaJuridicaLocal.remove(personaJuridica);
        } catch (Exception e) {
            personaJuridicaLocal = lookupPersonaJuridicaFacade();
            throw new Exception("Eliminar Persona Juridica. " + e.getMessage(), e);
        }
    }

    public void editarPersonaJuridica(PersonaJuridica personaJuridica) throws Exception {
        try {
            personaJuridicaLocal.edit(personaJuridica);
        } catch (Exception e) {
            personaJuridicaLocal = lookupPersonaJuridicaFacade();
            throw new Exception("Editar Persona Juridica. " + e.getMessage(), e);
        }
    }

    public PersonaJuridica buscarPersonaJuridica(long codigo) throws Exception {
        try {
            return personaJuridicaLocal.find(codigo);
        } catch (Exception e) {
            personaJuridicaLocal = lookupPersonaJuridicaFacade();
            throw new Exception("Buscar PersonaNatural. " + e.getMessage(), e);
        }
    }


    public List<PersonaJuridica> ListarPersonaJuridicas() throws Exception {
        try {
            return personaJuridicaLocal.findAll();
        } catch (Exception e) {
            personaJuridicaLocal = lookupPersonaJuridicaFacade();
            throw new Exception("Listar PersonaJuridica. " + e.getMessage(), e);
        }
    }

    
    
    
    
    //FACADES PERSONA , NATURA, JURIDICA
    private PersonaFacadeLocal lookupPersonaFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaFacadeLocal) c.lookup("java:comp/env/PersonaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private PersonaNaturalFacadeLocal lookupPersonaNaturalFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaNaturalFacadeLocal) c.lookup("java:comp/env/PersonaNaturalFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private PersonaJuridicaFacadeLocal lookupPersonaJuridicaFacade() {
        try {
            Context c = new InitialContext();
            return (PersonaJuridicaFacadeLocal) c.lookup("java:comp/env/PersonaJuridicaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
